﻿#include "journal.h"
#include "ui_journal.h"

Journal::Journal(QWidget *parent) :
    BaseEditor(parent),
    ui(new Ui::Journal)
{
    ui->setupUi(this);

    table = TD_TABLE;
    type = T_JOURNAL;

    dateModel=new QSqlQueryModel(this);
    ui->listView->setModel(dateModel);

    model=new QSqlTableModel(this);
    model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    ui->tableView->setModel(model);

    updateView();
    createMenu();
}

Journal::~Journal()
{
    delete ui;
}

void Journal::createMenu()
{
    menu=new QMenu(this);

    menu->addAction(QStringLiteral("查找..."),this,&Journal::search);
    menu->addAction(QStringLiteral("更新"),this,&Journal::updateView);
    menu->addSeparator();
    menu->addAction(QStringLiteral("撤销"),this,&Journal::revert);
    menu->addAction(QStringLiteral("保存"),this,&Journal::save);
    menu->addSeparator();
    menu->addAction(QStringLiteral("删除"),this,&Journal::removeItem);
}

void Journal::showSearchBox(const QString &condition)
{
    QString input=QInputDialog::getText(this,QStringLiteral("输入关键词"),QStringLiteral("关键词"));
    if(!input.isEmpty())
    {
        //把input放在最后可避免自动替换
        QString word=QString("type = '%1' AND %2 like '%%3%'").arg(type).arg(condition).arg(input);
        model->setFilter(word);
        model->select();
    }
}

bool Journal::addItem(const QString &label)
{
    QSqlQuery query;
    query.prepare(QString("insert into %1 (date,label,host,type) values (?,?,?,?)").arg(table));
    query.addBindValue(Env::today);
    query.addBindValue(label);
    query.addBindValue(Env::hostName);
    query.addBindValue(type);
    return query.exec();
}

void Journal::removeItem()
{
    QModelIndex index=ui->tableView->currentIndex();
    if(index.isValid())
    {
        QSqlRecord record=model->record(index.row());
        int id=record.value("id").toInt();
        QString host=getValue(table,"host",id);
        if(host != Env::hostName)
        {
            QMessageBox::warning(this,QStringLiteral("无权操作"),QStringLiteral("您无权操作他人的记录！"));
            return;
        }
        model->removeRow(index.row());
    }
}

void Journal::updateView()
{
    dateModel->setQuery(QString("SELECT date from %1 where type='%2' GROUP BY date DESC")
                        .arg(table)
                        .arg(type));
    dateModel->setHeaderData(0,Qt::Horizontal,QStringLiteral("日期"));

    updateEventView(Env::today);
}

void Journal::updateEventView(const QString &date)
{
    model->setTable(table);
    model->setFilter(QString("date = '%1' AND type='%2'").arg(date).arg(type));
    model->select();

    ui->tableView->hideColumn(0); //id
    ui->tableView->hideColumn(1); //date
    model->setHeaderData(2,Qt::Horizontal,QStringLiteral("事件"));    //label
    ui->tableView->hideColumn(3); //host
    ui->tableView->hideColumn(4); //type
}

void Journal::on_listView_clicked(const QModelIndex &index)
{
    if(index.isValid())
    {
        QSqlRecord record=dateModel->record(index.row());
        QString date=record.value("date").toString();
        updateEventView(date);
    }
}

void Journal::on_btn_submit_clicked()
{
    QString event=ui->event->text();
    if(event.isEmpty())
    {
        QMessageBox::warning(this,QStringLiteral("不能为空"),QStringLiteral("事件不能为空！"));
        return;
    }

    if(addItem(event))
    {
        updateView();
        ui->event->clear();
    }
    else
        QMessageBox::warning(this,QStringLiteral("异常情况"),QStringLiteral("无法提交数据！"));
}
